﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// DisplayFlow Page.
    /// </summary>
    public partial class DisplayFlowPage : Page
    {
        private int _flowId;

        protected Flow CurrentFlow
        {
            get;
            private set;
        }

        protected override void OnLoad(EventArgs e)
        {
            _flowId = QSUtil.GetPositiveInt32("FlowID");
            if (_flowId <= 0)
            {
                SiteContext.Current.Alert("未指定流程编号", true);
                return;
            }
            CurrentFlow = Flow.GetFlow(_flowId);
            if (CurrentFlow == null)
            {
                SiteContext.Current.Alert("指定流程不存在", true);
                return;
            }

            // Binds data source for the repeater.
            flowRepeater.DataSource = FlowStep.GetSteps(_flowId);
            flowRepeater.DataBind();

#if false
            Bind();
#endif

            base.OnLoad(e);
        }

#if false
        private void Bind()
        {
            var tt = new Table();

            tt.Style.Add("font-size", "10pt");
            tt.Width = Unit.Percentage(100);
            tt.HorizontalAlign = HorizontalAlign.Center;

            AddRow(tt, Flow.GetFlowTitle(_flowId), Color.FromArgb(250, 250, 250));

            var steps = FlowStep.GetSteps(_flowId);
            for (int i = 0; i < steps.Count; i++)
            {
                AddStep(tt, steps[i], i + 1);
                AddRow(tt, "<span lang='EN-US' style='font-size: 10.5pt; font-family: Wingdings'>ò</span>", Color.FromArgb(250, 250, 250));
            }

            AddRow(tt, "<a href='#' onclick='history.back();'>流程结束</a>", Color.FromArgb(250, 250, 250));

            Page.Controls.Add(tt);
        }

        private void AddRow(Table tab, string Label, string Caption)
        {
            var tr = new TableRow();
            var td1 = new TableCell();
            var td2 = new TableCell();

            td1.Text = Label;
            td1.HorizontalAlign = HorizontalAlign.Right;
            td1.BackColor = Color.FromArgb(240, 240, 240);
            td1.Style.Add("width", "100px");
            td2.Text = Caption;
            td2.HorizontalAlign = HorizontalAlign.Left;
            td2.Style.Add("width", "300px");

            tr.Cells.Add(td1);
            tr.Cells.Add(td2);

            tab.Rows.Add(tr);
        }

        private void AddRow(Table tab, string Label, string Caption, Color BackColor)
        {
            var tr = new TableRow();
            var td1 = new TableCell();
            var td2 = new TableCell();

            td1.Text = Label;
            td1.HorizontalAlign = HorizontalAlign.Right;
            td1.BackColor = BackColor;

            td2.Text = Caption;
            td2.HorizontalAlign = HorizontalAlign.Left;
            td2.BackColor = BackColor;

            td1.Style.Add("width", "100px");
            td2.Style.Add("width", "300px");


            tr.Cells.Add(td1);
            tr.Cells.Add(td2);

            tab.Rows.Add(tr);
        }

        private void AddRow(Table tab, string Label, Color BackColor)
        {
            var tr = new TableRow();
            var td = new TableCell();

            td.Text = Label;
            td.HorizontalAlign = HorizontalAlign.Center;
            td.BackColor = BackColor;

            tr.Cells.Add(td);

            tab.Rows.Add(tr);
        }

        private void AddStep(Table tab, FlowStep step, int Step)
        {
            var tt = new Table();
            var tr = new TableRow();
            var tc = new TableCell();
            long top = 300 + 500 * Step;

            tt.HorizontalAlign = HorizontalAlign.Center;
            tt.Style.Add("left", "100px");
            tt.Style.Add("top", top + "px");
            //tt.Style.Add("BORDER-COLLAPSE","collapse");
            tt.Style.Add("width", "400px");
            tt.Style.Add("font-size", "10pt");

            tt.BorderColor = Color.FromArgb(0, 0, 0);
            tt.BorderWidth = 1;


            AddRow(tt, "第" + Step + "步", step.Name, Color.FromArgb(255, 245, 245));
            AddRow(tt, "流程规则", Utility.GetEnumDisplayName(step.FlowRule));
            AddRow(tt, "强制结束", step.RightToFinish ? "能" : "否");
            if (step.PassNum > 0)
            {
                AddRow(tt, "是否会签", step.PassNum + "人会签");
            }
            else if (step.PassNum == 0)
            {
                AddRow(tt, "是否会签", "不需要会签");
            }
            else if (step.PassNum < 0)
            {
                AddRow(tt, "是否会签", "全体通过！");
            }

            tc.Controls.Add(tt);

            //添加条件跳转
            if (step.JumpCount > 0)
            {
                var df = new DocumentFlow();

                DataTable dt;
                df.GetJump(_flowId, Step, 0, out dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    AddRow(tt, "条件" + dt.Rows[i]["Priority"], dt.Rows[i]["Field_Description"] + dt.Rows[i]["compare"].ToString() + dt.Rows[i]["comparevalue"] + "<span lang='EN-US' style='font-family: Wingdings'>&eth;</span>" + dt.Rows[i]["step_name"]);
                }
            }

            tr.Cells.Add(tc);

            tab.Rows.Add(tr);
        }
#endif

        protected IList<FlowJump> GetJumps(FlowStep step)
        {
            return FlowJump.GetJumps(step.FlowID, step.ID);
        }
    }
}